package com.xuan.juc.lock;

import java.util.concurrent.TimeUnit;

public class DeadLockTest {
    public static void main(String[] args) {
        String lockA = "lockA";
        String lockB = "lockB";

        new Thread(new myThread(lockA,lockB)).start();
        new Thread(new myThread(lockB,lockA)).start();

    }
}

class myThread implements Runnable{
    private String lockA ;
    private String lockB ;

    public myThread(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){

            System.out.println(lockA);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println(lockB);
            }
        }


    }
}
